% LoadMonitorData: Load the data from DATE_START to DATE_END
% INPUTS: 
%   - ROOT_PATH: Root path of the data (the folder containing the years)
%   - DATE_START, DATE_END: structure DATE.year, DATE.month, DATE.day
%   - CHAN_NAME_LIST: string or cell array of string containing the list of
%     the names of the channel to be read
function [date, monitor_data] = LoadMonitorData(root_path, date_start, date_end, chan_name_list, n_decimate)
    
    if nargin == 4
        n_decimate = 1;
    end

    if date_start.year > date_end.year
       error('Start year must be before end year');
    elseif (date_start.year == date_end.year) && (date_start.month > date_end.month)
        error('Start month must be before end month');
    end
    
    folders = GetFolders(root_path, date_start, date_end);
    
    if isempty(folders)
        date = [];
        monitor_data = [];
    else
        % Read HDF5 files in folders
        ind = 1;
        
        h = waitbar(0,'Reading files, please wait...');
        for k = 1:length(folders) 
            
            if mod(k,n_decimate) == 0
                listing = dir(folders{k});
                for i = 1:length(listing)                   
                    if ~listing(i).isdir
                        
                        [name,extension] = strtok(listing(i).name, '.');
                        tmp = strtok(name,'_');
                        
                        if strcmp(extension, '.h5') && strcmp(tmp,'run')
                            h5filename = sprintf('%s/%s',folders{k},listing(i).name);
                            
                            if IsMonitor(h5filename) % Test if Monitor data are present in the HDF5 file
                                data = ReadMonitorData(h5filename, chan_name_list);
                                if iscell(data)
                                    monitor_data(ind,:) = cell2mat(data);
                                else
                                    monitor_data(ind) = data;
                                end
                                date(ind) = listing(i).datenum;
                                ind = ind+1;
                            end
                        end
                    end
                end
            end
            
            if mod(k,10) == 0
                waitbar(k/length(folders),h)
            end
        end
        close(h)
    end
end % End LoadMonitorData

% GetFolders: Identify folders of the relevant days
% folders: structure contaning the folder paths
function folders = GetFolders(root_path, date_start, date_end)
    listing_root = dir(root_path);
    
    folders = {};
    
    ind_fold = 1;
    
    h = waitbar(0,'Selecting folders, please wait...');
    for i = 3:length(listing_root) % Scan years
        if listing_root(i).isdir            
            if (str2num(listing_root(i).name) >= date_start.year) && (str2num(listing_root(i).name) <= date_end.year)
                month_path = sprintf('%s/%s', root_path, listing_root(i).name);
                listing_month = dir(month_path);
                
                for j = 3:length(listing_month) % Scan months
                    if listing_month(j).isdir

                        if (str2num(listing_root(i).name) == date_start.year) && (str2num(listing_month(j).name) < date_start.month)
                            continue;
                        elseif (str2num(listing_root(i).name) == date_end.year) && (str2num(listing_month(j).name) > date_end.month)
                            continue;
                        end
                        
                        day_path = sprintf('%s/%s', month_path, listing_month(j).name);
                        listing_day = dir(day_path);
                    
                        for k = 3:length(listing_day) % Scan days
                            if listing_day(k).isdir
                                if (str2num(listing_root(i).name) == date_start.year) ...
                                   && (str2num(listing_month(j).name) == date_start.month) ...
                                   && (str2num(listing_day(k).name) < date_start.day)
                                    continue;
                                elseif (str2num(listing_root(i).name) == date_end.year) ...
                                   && (str2num(listing_month(j).name) == date_end.month) ...
                                   && (str2num(listing_day(k).name) > date_end.day)
                                    continue;
                                end
                                
                                intraday_path = sprintf('%s/%s',day_path,listing_day(k).name);
                                listing_intraday = dir(intraday_path);
                                
                                for l = 3:length(listing_intraday) % Scan intraday
                                    if listing_intraday(l).isdir
                                        folders{ind_fold} = sprintf('%s/%s',intraday_path,listing_intraday(l).name);
                                        ind_fold = ind_fold + 1;
                                    end
                                end % Intraday
                            end
                        end % Days
                    end
                end % Months
            end
        end        
        waitbar(i/length(listing_root),h)    
    end % Years
    close(h)
end % End GetFolders

